import { Global } from "./Global";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.SpriteAtlas)
    atlas: cc.SpriteAtlas = null;



    onLoad() {
        this.changeType(Global.type);
    }

    changeType(type: number) {
        type = this.checkType(type);

        let sprite = this.node.getComponent(cc.Sprite);
        sprite.spriteFrame = this.atlas.getSpriteFrames()[type];
        let physic = this.node.getComponent(cc.PhysicsCircleCollider);
        physic.tag = type;
        physic.radius = this.node.width / 2;
        physic.apply();
    }

    checkType(type: number) {
        if (type < 0) return 0;
        if (type > 10) return 10;
        return type;
    }

    // update (dt) {}
    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        cc.log('concat');
        if (other.node.group == 'default' || self.tag != other.tag) {
            return;
        }
        if (self.node.y < other.node.y) {
            other.node.destroy();
            this.waterboom(self.tag);
        }
        else {
            this.node.destroy();
        }
    }

    waterboom(type: number) {
        this.changeType(type + 1);
        this.node.getComponent(cc.AudioSource).play();
    }
}
